package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * www.voc.com.cn 华声论坛
 *
 * @author KK
 */
public class RednetBbsAnalyse implements AnalyseNews {
	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://bbs.rednet.cn/thread-[0-9]*-1-1.html",
				"http://cs.rednet.cn/thread-[0-9]*-1-1.html",
				"http://bbs.rednet.cn/forum.php?mod=viewthread&tid=[0-9]*&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26orderby%3Ddateline"
		};
		//http://bbs.rednet.cn/forum.php?mod=viewthread&tid=23623618&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26orderby%3Ddateline
		//http://bbs.rednet.cn/forum.php?mod=viewthread&tid=44356747&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26orderby%3Ddateline
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		String htmltxt=urlMeta.getHtml();
		urlMeta=CrawlHTML.responseToURL(url);
		htmltxt=urlMeta.getHtml();
		if(htmltxt==null){
		}
		NewsMeta news = new NewsMeta();
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String updateUrl = url;
		Document doc = Jsoup.parse(htmltxt);
		Elements select = doc.select("div#messagetext.alert_error");

		if (select.size() > 0) {
			news.setUpdateUrl(ParseState.FORBID.toString());
		}
		title = doc.select("h1.ts a#thread_subject").text();
		if (StringUtils.isBlank(title)) {
			title = doc.select("title").text();
			if (StringUtils.isNotEmpty(title)) {
				title = title.replace("红网论坛", "");
				title = StringUtils.substringBefore(title, "-");
			}
		}

		String text = doc.select("div#postlist table div.hm").text();
		String regex = "查看: ([0-9]*)[|]回复: ([0-9]*)";
		if (text.matches(regex)) {
			clickNum = RegexUtils.getMatchGroupString(text, regex, 1);
			commnetNum = RegexUtils.getMatchGroupString(text, regex, 2);
		}

		Elements divs = doc.select("div#ct > div#postlist > div[id]");
		Element div1;
		if (divs.size() > 0) {
			div1 = divs.get(0);
			dateStr = div1.select("div.authi em span").attr("title");
			if (StringUtils.isNullOrEmpty(dateStr)) {
				dateStr = div1.select("div.authi").text();
			}
			content = HtmlCleaner.getContentHtml(url,  div1.select("div.pcb"));
		}

		date = DateUtils.matchDate(dateStr);
		if(date==null){
			date = DateUtils.matchDate(doc.select("div.authi em").text());
		}

			news.setUrl(url);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(updateUrl);

			String author=null;
			Elements select2 = doc.select("div#postlist tbody tr td.pls div.pi div.authi");
			if(select2!=null && select2.size()!=0){
				author=select2.get(0).text();
			}
			news.setAuthor(author);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)) {
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	
}
